﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;

namespace FutianCloudAPIService.Utils
{
    public class ImageUtil
    {

        public static string ImgToBase64(Bitmap  bmp )
        {
            using (var m = new MemoryStream())
            {
                bmp.Save(m, bmp.RawFormat);

                byte[] imageBytes = m.ToArray();

                string base64String = Convert.ToBase64String(imageBytes);


                //return "data:image/jpeg;base64," + base64String;

                return base64String;
            }
        }

        public static Bitmap ToGray(Bitmap bmp)
        {
            for (int i = 0; i < bmp.Width; i++)
            {
                for (int j = 0; j < bmp.Height; j++)
                {
                    //获取该点的像素的RGB的颜色
                    Color color = bmp.GetPixel(i, j);
                    //利用公式计算灰度值
                    int gray = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);
                    Color newColor = Color.FromArgb(gray, gray, gray);
                    bmp.SetPixel(i, j, newColor);
                }
            }
            return bmp;
        }
    }
}
